﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Configuration;
using Newtonsoft.Json;

namespace MechanicsLab
{
    public class TstPress
    {
        string connStr = "";

        public TstPress(string path)
        {
            connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Persist Security Info=False;";
        }

        #region 压力机
        public string PostData(string precativeNum, string name, string remark_1)
        {
            List<PostDataPress> postdata = new List<PostDataPress>();
            DataTable dr = SelPostInfo(precativeNum);
            if (dr != null)
            {
                if (dr.Rows.Count > 0)
                {
                    for (int b = 0; b < dr.Rows.Count; b++)
                    {
                        PostDataPress chp = new PostDataPress();
                        chp.trialName = name;
                        chp.equNum = ConfigurationManager.AppSettings["Number"];
                        //一组数据ID
                        chp.ID = remark_1;
                        //试验单号
                        chp.Number = dr.Rows[b]["试样编号"].ToString();
                        //力值
                        chp.Force1 = dr.Rows[b]["最大力1"].ToString();
                        chp.Force2 = dr.Rows[b]["最大力2"].ToString();
                        chp.Force3 = dr.Rows[b]["最大力3"].ToString();
                        //抗压值
                        chp.Compress1 = dr.Rows[b]["抗压强度1"].ToString();
                        chp.Compress2 = dr.Rows[b]["抗压强度2"].ToString();
                        chp.Compress3 = dr.Rows[b]["抗压强度3"].ToString();

                        #region 曲线值

                        for (int j = 0; j < Pub.Conctete.Length; j++)
                        {
                            if (File.Exists(ConfigurationManager.AppSettings["Curve"] + dr.Rows[b]["ID"].ToString() + @"\" + Pub.Conctete[j]))
                            {
                                StreamReader sr = new StreamReader(ConfigurationManager.AppSettings["Curve"] + dr.Rows[b]["ID"].ToString() + @"\" + Pub.Conctete[j], Encoding.Default);
                                string line;
                                int i = 0;
                                while ((line = sr.ReadLine()) != null)
                                {
                                    string[] tt = line.Replace("\t\t", "|").Split('|');
                                    if (i > 2)
                                    {
                                        if (j == 0)
                                        {
                                            chp.Curve1.Add(tt[0] + "," + tt[1]);
                                        }
                                        else if (j == 1)
                                        {
                                            chp.Curve2.Add(tt[0] + "," + tt[1]);
                                        }
                                        else
                                        {
                                            chp.Curve3.Add(tt[0] + "," + tt[1]);
                                        }
                                    }
                                    i++;
                                }
                                sr.Close();
                                sr.Dispose();
                            }
                        }
                        #endregion
                        postdata.Add(chp);
                    }
                }
            }

            string jsonstr = JsonConvert.SerializeObject(postdata);
            string str = Pub.HttpPost(ConfigurationManager.AppSettings["PostDataUrl"], jsonstr);
            return str;
        }

        public DataTable SelPostInfo(string precativeNum)
        {
            DataTable dt = null;
            try
            {
                string sql = "select * from Concrete where 试样编号='" + precativeNum + "' and 完成状态1='已完成' and 完成状态2='已完成'and 完成状态3='已完成' and 完成状态='已完成' ";
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        #endregion

        #region 万能机
        public string PostData1(string precativeNum, string name, string remark_1)
        {
            List<PostDataPress> postdata = new List<PostDataPress>();
            DataTable dr = SelPostInfo1(precativeNum);
            if (dr != null)
            {
                if (dr.Rows.Count > 0)
                {
                    for (int b = 0; b < dr.Rows.Count; b++)
                    {
                        PostDataPress chp = new PostDataPress();
                        chp.trialName = name;
                        chp.equNum = ConfigurationManager.AppSettings["Number"];
                        //一组数据ID
                        chp.ID = dr.Rows[b]["Num"].ToString();
                        //试验单号
                        chp.Number = dr.Rows[b]["编号"].ToString();
                        //力值
                        chp.Force1 = dr.Rows[b]["上屈服力"].ToString();
                        chp.Force2 = dr.Rows[b]["下屈服力"].ToString();
                        //抗压值
                        chp.Compress1 = dr.Rows[b]["上屈服强度"].ToString();
                        chp.Compress2 = dr.Rows[b]["下屈服强度"].ToString();

                        #region 曲线值
                        if (File.Exists(ConfigurationManager.AppSettings["Curve"] + dr.Rows[b]["Num"].ToString() + ".crv"))
                        {
                            StreamReader sr = new StreamReader(ConfigurationManager.AppSettings["Curve"] + dr.Rows[b]["Num"].ToString() + ".crv", Encoding.Default);
                            string line;
                            int i = 0;
                            while ((line = sr.ReadLine()) != null)
                            {
                                string[] tt = line.Replace("\t\t", "|").Split('|');
                                if (i > 2)
                                {
                                    chp.Curve1.Add(tt[6] + "," + tt[0]);
                                }
                                i++;
                            }
                            sr.Close();
                            sr.Dispose();
                        }
                        #endregion
                        postdata.Add(chp);
                    }
                }
            }
            string jsonstr = JsonConvert.SerializeObject(postdata);
            string str = Pub.HttpPost(ConfigurationManager.AppSettings["PostDataUrl"], jsonstr);
            return str;
        }

        public DataTable SelPostInfo1(string precativeNum)
        {
            DataTable dt = null;
            try
            {
                string sql = "select * from Tension where 编号='" + precativeNum + "'";
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        #endregion

        #region 压力抗折抗压
        public string PostData2(string precativeNum, string name, string remark_1)
        {
            List<PostDataPress> postdata = new List<PostDataPress>();
            DataTable dr = SelPostInfo2(precativeNum);
            if (dr != null)
            {
                if (dr.Rows.Count > 0)
                {

                    for (int b = 0; b < dr.Rows.Count; b++)
                    {
                        PostDataPress chp = new PostDataPress();
                        chp.trialName = name;
                        chp.equNum = ConfigurationManager.AppSettings["Number"];
                        if (name.Equals("水泥物理力学性能试验"))
                        {
                            //一组数据ID
                            chp.ID = dr.Rows[b]["ID"].ToString();
                            //试验单号
                            chp.Number = dr.Rows[b]["试样编号"].ToString();
                            //力值
                            chp.Force1 = dr.Rows[b]["抗折载荷1"].ToString();
                            chp.Force2 = dr.Rows[b]["抗折载荷2"].ToString();
                            chp.Force3 = dr.Rows[b]["抗折载荷3"].ToString();

                            chp.Force4 = dr.Rows[b]["最大力1"].ToString();
                            chp.Force5 = dr.Rows[b]["最大力2"].ToString();
                            chp.Force6 = dr.Rows[b]["最大力3"].ToString();
                            chp.Force7 = dr.Rows[b]["最大力4"].ToString();
                            chp.Force8 = dr.Rows[b]["最大力5"].ToString();
                            chp.Force9 = dr.Rows[b]["最大力6"].ToString();
                            //抗压值
                            chp.Compress1 = dr.Rows[b]["抗折强度1"].ToString();
                            chp.Compress2 = dr.Rows[b]["抗折强度2"].ToString();
                            chp.Compress3 = dr.Rows[b]["抗折强度3"].ToString();

                            chp.Compress4 = dr.Rows[b]["抗压强度1"].ToString();
                            chp.Compress5 = dr.Rows[b]["抗压强度2"].ToString();
                            chp.Compress6 = dr.Rows[b]["抗压强度3"].ToString();
                            chp.Compress7 = dr.Rows[b]["抗压强度4"].ToString();
                            chp.Compress8 = dr.Rows[b]["抗压强度5"].ToString();
                            chp.Compress9 = dr.Rows[b]["抗压强度6"].ToString();

                            #region 曲线值

                            for (int j = 0; j < Pub.Conctete1.Length; j++)
                            {
                                if (File.Exists(ConfigurationManager.AppSettings["Curve"] + dr.Rows[b]["ID"].ToString() + @"\" + Pub.Conctete1[j]))
                                {
                                    StreamReader sr = new StreamReader(ConfigurationManager.AppSettings["Curve"] + dr.Rows[b]["ID"].ToString() + @"\" + Pub.Conctete1[j], Encoding.Default);
                                    string line;
                                    int i = 0;
                                    while ((line = sr.ReadLine()) != null)
                                    {
                                        string[] tt = line.Replace("\t\t", "|").Split('|');
                                        if (i > 2)
                                        {
                                            if (j == 0)
                                            {
                                                chp.Curve1.Add(tt[0] + "," + tt[1]);
                                            }
                                            else if (j == 1)
                                            {
                                                chp.Curve2.Add(tt[0] + "," + tt[1]);
                                            }
                                            else if (j == 2)
                                            {
                                                chp.Curve3.Add(tt[0] + "," + tt[1]);
                                            }
                                            else if (j == 3)
                                            {
                                                chp.Curve4.Add(tt[0] + "," + tt[1]);
                                            }
                                            else if (j == 4)
                                            {
                                                chp.Curve5.Add(tt[0] + "," + tt[1]);
                                            }
                                            else if (j == 5)
                                            {
                                                chp.Curve6.Add(tt[0] + "," + tt[1]);
                                            }
                                            else if (j == 6)
                                            {
                                                chp.Curve7.Add(tt[0] + "," + tt[1]);
                                            }
                                            else if (j == 7)
                                            {
                                                chp.Curve8.Add(tt[0] + "," + tt[1]);
                                            }
                                            else
                                            {
                                                chp.Curve9.Add(tt[0] + "," + tt[1]);
                                            }
                                        }
                                        i++;
                                    }
                                    sr.Close();
                                    sr.Dispose();
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            //一组数据ID
                            chp.ID = dr.Rows[b]["ID"].ToString();
                            //试验单号
                            chp.Number = dr.Rows[b]["试样编号"].ToString();
                            //力值
                            chp.Force1 = dr.Rows[b]["最大力1"].ToString();
                            chp.Force2 = dr.Rows[b]["最大力2"].ToString();
                            chp.Force3 = dr.Rows[b]["最大力3"].ToString();
                            //抗压值
                            chp.Compress1 = dr.Rows[b]["抗压强度1"].ToString();
                            chp.Compress2 = dr.Rows[b]["抗压强度2"].ToString();
                            chp.Compress3 = dr.Rows[b]["抗压强度3"].ToString();

                            #region 曲线值

                            for (int j = 0; j < Pub.Conctete.Length; j++)
                            {
                                if (File.Exists(ConfigurationManager.AppSettings["Curve"] + dr.Rows[b]["ID"].ToString() + @"\" + Pub.Conctete[j]))
                                {
                                    StreamReader sr = new StreamReader(ConfigurationManager.AppSettings["Curve"] + dr.Rows[b]["ID"].ToString() + @"\" + Pub.Conctete[j], Encoding.Default);
                                    string line;
                                    int i = 0;
                                    while ((line = sr.ReadLine()) != null)
                                    {
                                        string[] tt = line.Replace("\t\t", "|").Split('|');
                                        if (i > 2)
                                        {
                                            if (j == 0)
                                            {
                                                chp.Curve1.Add(tt[0] + "," + tt[1]);
                                            }
                                            else if (j == 1)
                                            {
                                                chp.Curve2.Add(tt[0] + "," + tt[1]);
                                            }
                                            else
                                            {
                                                chp.Curve3.Add(tt[0] + "," + tt[1]);
                                            }
                                        }
                                        i++;
                                    }
                                    sr.Close();
                                    sr.Dispose();
                                }
                            }
                            #endregion
                        }
                        postdata.Add(chp);
                    }
                }
            }
            string jsonstr = JsonConvert.SerializeObject(postdata);
            string str = Pub.HttpPost(ConfigurationManager.AppSettings["PostDataUrl"], jsonstr);
            return str;
        }

        public DataTable SelPostInfo2(string precativeNum)
        {
            DataTable dt = null;
            try
            {
                string sql = "select * from Cement where 试样编号='" + precativeNum + "'";
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        #endregion

        public DataTable Query(string sql)
        {
            OleDbConnection conn = null;
            try
            {
                DataTable table = new DataTable();
                conn = new OleDbConnection(connStr);
                conn.Open();
                using (OleDbDataAdapter da = new OleDbDataAdapter(sql, conn))
                {
                    da.Fill(table);
                }
                return table;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }


}
